//
//  UIBarButtonItem+Extension.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/10.
//  Copyright © 2015年 郭平. All rights reserved.
//

import Foundation
import UIKit

extension UIBarButtonItem {
    
    class func item(imageName: String = "", title: String = "", target: AnyObject?, action: Selector) -> UIBarButtonItem  {
        
        // 初始化一个按钮
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        // 判断title是否有文字
        if title.characters.count > 0 {
            
            button.setTitle(title, forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            button.setTitleColor(UIColor(red: 80/255, green: 80/255, blue: 80/255, alpha: 1), forState: UIControlState.Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        }
        
        if imageName.characters.count > 0 {
            
            button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        }
        // 调整按钮大小
        button.sizeToFit()
        
        return UIBarButtonItem(customView: button)
    }
    
}